//
//  TestViewController.m
//  ShandsMod
//
//  Created by zh_s on 3/17/23.
//  Copyright © 2023 zh_s. All rights reserved.
// https://github.com/ShaeZhuJiu/UISegmentedControl_base

#import "TestViewController.h"
#import "GWVerisonTipsView.h"
#import "SubViewController.h"
#import "MiningVC.h"

@interface TestViewController () <UIScrollViewDelegate>

/**! 不在当前区域*/
@property (nonatomic, strong) UIView *areaOutV;
@property (nonatomic, strong) UIView *joinTestV;
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UISegmentedControl *segmentedControl;

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav.hidden = YES;
    self.navTitle = @"1111";
//
//    [self createNoDataView];
//
//    return;
    [self.view addSubview:[self scrollView]];


    for (int i = 0; i<2; i++) {
        if ( i == 0) {
            SubViewController *sub=[[SubViewController alloc]init];
            sub.view.frame = CGRectMake(0, 0,SCREEN_WIDTH , SCREEN_HEIGHT);
            [self.scrollView addSubview:sub.view];
            [self addChildViewController:sub];
        }else {

            MiningVC *sub= [[MiningVC alloc]init];
            sub.view.frame = CGRectMake(SCREEN_WIDTH, 0,SCREEN_WIDTH , SCREEN_HEIGHT);
            [self.scrollView addSubview:sub.view];
            [self addChildViewController:sub];
        }


    }
    UIView *segmentV = [BaseViewFactory viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT + 52) color:UIColor.clearColor];
    [self.view addSubview:segmentV];
    [segmentV addSubview:self.segmentedControl];
       
    [_segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(STATUS_BAR_HEIGHT+10);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(158);
        
    }];
//    self.nav.hidden = YES;
    
    // Do any additional setup after loading the view.
}
- (UIScrollView *)scrollView{
    if (_scrollView==nil) {
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _scrollView.contentSize=CGSizeMake(SCREEN_WIDTH*2, 0);
        _scrollView.backgroundColor=[UIColor clearColor];
        _scrollView.pagingEnabled=YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (UISegmentedControl *)segmentedControl{
    if (_segmentedControl == nil) {
        _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"梯子",@"挖矿"]];
        _segmentedControl.selectedSegmentIndex = 0;

//        [_segmentedControl setBackgroundImage:IMAGE_NAME(@"icon_test_segment_nol")
//                                             forState:UIControlStateNormal
//                                           barMetrics:UIBarMetricsDefault];
//        [_segmentedControl setBackgroundImage:IMAGE_NAME(@"icon_test_segment")
//                                         forState:UIControlStateSelected
//                                       barMetrics:UIBarMetricsDefault];
//
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor],NSForegroundColorAttributeName,nil];
        [_segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
        
//        NSDictionary *titleColorDic = [NSDictionary dictionaryWithObjectsAndKeys:UIColor.blueColor,NSForegroundColorAttributeName,nil];
//        [_segmentedControl setTitleTextAttributes:titleColorDic forState:UIControlStateSelected];
        for (id segment in [_segmentedControl subviews]) {
            for (id label in [segment subviews]) {
                if ([label isKindOfClass:[UILabel class]]) {
                    UILabel *titleLabel = (UILabel *) label;
                    [titleLabel setTextColor:[UIColor whiteColor]];

                }
            }
        }
//        _segmentedControl.text
        _segmentedControl.backgroundColor = MAIN_BACKCOLOR;
        [_segmentedControl addTarget:self action:@selector(changeIndex) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX=self.scrollView.contentOffset.x;
    CGFloat x=offsetX/SCREEN_WIDTH;
    int index=(int)round(x);
    self.segmentedControl.selectedSegmentIndex=index;
}
-(void)changeIndex{
    NSInteger index=self.segmentedControl.selectedSegmentIndex;
    self.scrollView.contentOffset=CGPointMake(index*SCREEN_WIDTH, 0);
}

- (void)createAreaOutView {
    UIView *areaView = [BaseViewFactory viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 117) color:MAIN_BACKCOLOR];
    [self.view addSubview:areaView];
    areaView.backgroundColor = UIColor.clearColor;
    _areaOutV = areaView;
    
    UIImageView *iconImg = [[UIImageView alloc] init];
    [areaView addSubview:iconImg];
    iconImg.image = IMAGE_NAME(@"icon_test_areaOut");
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.centerY.mas_equalTo(areaView.mas_centerY).mas_offset(-80);
        make.height.width.mas_equalTo(170);
    }];
    
    UILabel *tipsLab = [BaseViewFactory labelWithFrame:CGRectZero textColor:RGB(118, 118, 118) font:FONT_NAME(FONT_Regular, 14) textAligment:NSTextAlignmentCenter andtext:@"温馨提示：该功能不支持在当前区域使用"];
    [areaView addSubview:tipsLab];
    [tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.top.mas_equalTo(iconImg.mas_bottom).mas_offset(55);
        make.height.mas_equalTo(21);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    UIButton *nextBtn = [BaseViewFactory buttonWithFrame:CGRectZero font:FONT_NAME(FONT_Medium, 16) title:@"了解详情" titleColor:UIColor.whiteColor backColor:MAIN_ACTIVE_COLOR];
    [areaView addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.top.mas_equalTo(tipsLab.mas_bottom).mas_offset(35);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(200);
    }];
    GWRadius(nextBtn, 20);
    [nextBtn addTarget:self action:@selector(detailBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)detailBtnClick {
    SHOW_TIPS(@"了解详情");
}

- (void)createNoDataView {
    UIView *areaView = [BaseViewFactory viewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 117) color:MAIN_BACKCOLOR];
    [self.view addSubview:areaView];
    areaView.backgroundColor = UIColor.clearColor;
    _joinTestV = areaView;
    
    UIImageView *iconImg = [[UIImageView alloc] init];
    [areaView addSubview:iconImg];
    iconImg.image = IMAGE_NAME(@"bg_text_alert");
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.centerY.mas_equalTo(areaView.mas_centerY).mas_offset(-60);
        make.height.width.mas_equalTo(170);
    }];
    GWRadius(iconImg, 30);
    
    UILabel *tipsLab = [BaseViewFactory labelWithFrame:CGRectZero textColor:MAIN_BACKCOLOR font:FONT_NAME(FONT_Regular, 14) textAligment:NSTextAlignmentCenter andtext:@"这是一个NET名称"];
    [areaView addSubview:tipsLab];
    [tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.top.mas_equalTo(iconImg.mas_bottom).mas_offset(15);
        make.height.mas_equalTo(21);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    UIButton *nextBtn = [BaseViewFactory buttonWithFrame:CGRectZero font:FONT_NAME(FONT_Medium, 16) title:@"参与内侧" titleColor:UIColor.whiteColor backColor:MAIN_ACTIVE_COLOR];
    [areaView addSubview:nextBtn];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(areaView.mas_centerX);
        make.top.mas_equalTo(tipsLab.mas_bottom).mas_offset(45);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(200);
    }];
    GWRadius(nextBtn, 20);
    [nextBtn addTarget:self action:@selector(joinTestClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)joinTestClick {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    GWVerisonTipsView *tipsView = [[GWVerisonTipsView alloc] init];
    tipsView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [window addSubview:tipsView];
    __block GWVerisonTipsView *block_tipsView = tipsView;
    tipsView.block = ^(NSInteger index) {
        [block_tipsView removeFromSuperview];
        if (index == 100) {
            SHOW_TIPS(@"立即参与");
            [self changeTabbarState];
        }else{
            SHOW_TIPS(@"暂不参与");
            
        }
    };
}

- (void)changeTabbarState {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTabbarState" object:self userInfo:nil];
}

@end
